package com.martini.satoken01.mq.consumer;

import com.martini.satoken01.mq.constant.DestinationConstant;
import com.martini.satoken01.mq.model.SendEmailEventDO;
import com.martini.satoken01.service.EmailService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author martini at 2021/12/23 15:42
 */
@Service
@RocketMQMessageListener(topic = DestinationConstant.EMAIL_TOPIC, selectorExpression = DestinationConstant.EMAIL_TAG, consumerGroup = "EMAIL_EVENT_CONSUMER")
public class SendEmailEventMessageConsumer implements RocketMQListener<SendEmailEventDO> {

    private final EmailService emailService;

    public SendEmailEventMessageConsumer(EmailService emailService) {
        this.emailService = emailService;
    }

    @Override
    public void onMessage(SendEmailEventDO sendEmailEventDO) {
        emailService.sendEmail(sendEmailEventDO.getFrom(), sendEmailEventDO.getTo(), sendEmailEventDO.getSubject(), sendEmailEventDO.getText(), Optional.of(sendEmailEventDO.getHtml()).orElse(true));
    }

}
